package io.jft.doll.ui.room.detailFragment

import com.g.base.api.ErrorCode
import com.g.base.extend.observeExOnce
import com.g.base.extend.resultNotNull
import com.g.base.room.repository.Status
import com.g.base.ui.recyclerView.setupRecyclerView
import io.jft.doll.R
import io.jft.doll.databinding.ViewRecycleBinding
import io.jft.doll.room.entity.remote.RoomGameRankRecentREntity
import io.jft.doll.ui.room.bind.RoomRankRecentBind

/**
 * Created by G on 2017/12/21 0021.
 */
class RoomRecentFragment : RoomBaseFragment<ViewRecycleBinding>() {
    override fun setContentView(): Int = R.layout.view_recycle
    val adapter by lazy { setupRecyclerView(contentView.recyclerView) }

    override fun onLazyLoadOnce() {
        getData()
    }

    override fun getData() {
        roomViewModel.getRoomRecent()
                .resultNotNull()
                .observeExOnce(this) {
                    when (it.status) {
                        Status.Loading -> showLoading()
                        Status.Content -> setupView(it.data!!)
                        Status.Error -> {
                            if (it.error?.code == ErrorCode.EMPTY)
                                showEmpty()
                            else
                                showError(it.error?.message)
                        }
                    }
                }
    }

    private fun setupView(data: List<RoomGameRankRecentREntity>) {
        adapter.renderItems.addAll(data.map {
            RoomRankRecentBind(it, -1)
        })
        adapter.notifyDataSetChanged()
        showContentView()
    }
}